package app.Disthanitian.thaistyle.EmerMez;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ContactAdapter extends ArrayAdapter<MemberContact> {

	
	public ContactAdapter(Context context, int textViewResourceId,List<MemberContact> objects) { //List<MemberContact> objects
		super(context, textViewResourceId,objects);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		// retrieve the Person object binded at this position
		final MemberContact p = getItem(position);

		// A ViewHolder keeps references to children views to avoid unneccessary
		// calls
		// to findViewById() on each row.
		ViewHolder holder;

		// When convertView is not null, we can reuse it directly, there is no
		// need
		// to reinflate it. We only inflate a new View when the convertView
		// supplied
		// by ListView is null.
		if (convertView == null) {
			convertView = View.inflate(getContext(), R.layout.listview_item_rows, null);

			// Creates a ViewHolder and store references to the three children
			// views
			// we want to bind data to.
			holder = new ViewHolder();
			holder.textName = (TextView) convertView.findViewById(R.id.txtName);
			holder.textEmail = (TextView) convertView.findViewById(R.id.txtEmail);
			holder.textPhone = (TextView) convertView.findViewById(R.id.txtPhone);
			holder.picture = (ImageView) convertView.findViewById(R.id.imgIcon);
			holder.picture.setFocusable(false);
			holder.picture.setFocusableInTouchMode(false);
			convertView.setTag(holder);
		} else {
			// Get the ViewHolder back to get fast access to the TextView
			// and the ImageView.
			holder = (ViewHolder) convertView.getTag();
		}
       
		// Bind the data efficiently with the holder.
		holder.textName.setText("Name: "+p.getContactName());
		holder.textEmail.setText("Email:"+p.getContactEmail());
		holder.textPhone.setText("Phone:"+p.getContactPhone());
		//holder.picture.setImageResource(p.getResImage());
		//if(p.getUriPhoto()!=null){
		holder.picture.setImageURI(p.getUriPhoto());

		//holder.picture.setImageBitmap(p.getBitmap());
		//}else{
	    //holder.picture.setImageResource(R.drawable.imagebg);
		//}
		// click on the picture
		holder.picture.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(getContext(),"Clicked on " + p.getContactName() ,Toast.LENGTH_SHORT).show();

			}
		});

		return convertView;
	}
	
	static class ViewHolder {
		TextView textName, textEmail,textPhone;
		ImageView picture;
	}
}
